package com.gitee.swsk33.algorithms.sort;

import com.gitee.swsk33.algorithms.sort.util.AdvanceSort;
import com.gitee.swsk33.algorithms.sort.util.BasicSort;

import java.util.Arrays;
import java.util.Random;

public class SortTest {

	private static void printArray(int[] array) {
		for (int item : array) {
			System.out.print(item + " ");
		}
		System.out.println();
	}

	/**
	 * 返回一个无序的随机数组成的数组
	 *
	 * @param start 随机数范围起始值
	 * @param end   随机数范围终止值
	 * @param count 数组长度
	 * @return 随机数数组
	 */
	private static int[] randomArray(int start, int end, int count) {
		Random random = new Random();
		int[] array = new int[count];
		for (int i = 0; i < count; i++) {
			array[i] = random.nextInt(end + 1) + start;
		}
		return array;
	}

	public static void main(String[] args) {
		int[] array = randomArray(-100000, 200000, 75000);
		int[] s = Arrays.copyOf(array, array.length);
		long start, done;
		// 1.冒泡排序
		start = System.currentTimeMillis();
		BasicSort.bubbleSort(s);
		done = System.currentTimeMillis();
		System.out.println("冒泡排序用时：" + (done - start) + "ms");
		// 2.选择排序
		s = Arrays.copyOf(array, array.length);
		start = System.currentTimeMillis();
		BasicSort.selectSort(s);
		done = System.currentTimeMillis();
		System.out.println("选择排序用时：" + (done - start) + "ms");
		// 3.插入排序
		s = Arrays.copyOf(array, array.length);
		start = System.currentTimeMillis();
		BasicSort.insertSort(s);
		done = System.currentTimeMillis();
		System.out.println("插入排序用时：" + (done - start) + "ms");
		// 4.希尔排序
		s = Arrays.copyOf(array, array.length);
		start = System.currentTimeMillis();
		AdvanceSort.shellSort(s);
		done = System.currentTimeMillis();
		System.out.println("希尔排序用时：" + (done - start) + "ms");
		// 5.堆排序
		s = Arrays.copyOf(array, array.length);
		start = System.currentTimeMillis();
		AdvanceSort.heapSort(s);
		done = System.currentTimeMillis();
		System.out.println("堆排序用时：" + (done - start) + "ms");
		// 6.递归的归并排序
		s = Arrays.copyOf(array, array.length);
		start = System.currentTimeMillis();
		AdvanceSort.mergeSortR(s);
		done = System.currentTimeMillis();
		System.out.println("递归的归并排序用时：" + (done - start) + "ms");
		// 7.迭代的归并排序
		s = Arrays.copyOf(array, array.length);
		start = System.currentTimeMillis();
		AdvanceSort.mergeSort(s);
		done = System.currentTimeMillis();
		System.out.println("迭代的归并排序用时：" + (done - start) + "ms");
		// 8.快速排序
		s = Arrays.copyOf(array, array.length);
		start = System.currentTimeMillis();
		AdvanceSort.quickSort(s);
		done = System.currentTimeMillis();
		System.out.println("快速排序用时：" + (done - start) + "ms");
		printArray(s);
	}

}